﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CentaLine.Infrastructure;
using CentaLine.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Configuration;


namespace CentaLine.DAL
{
    public class DirectorDAL
    {
        Database db = DBHelper.CreateDataBase();
        Database dbOAS = DBHelper.CreateDataBase("CentalineConnectOAS"); //人事信息库

        #region modify by sunlf 此处废弃 改用GetAllDirectorListFromOASDAL 获取  2013-10-14 10:21:31   获取有账号下有虚拟号码的区董信息列表
        /// <summary>
        /// 获取有账号下有虚拟号码的区董信息列表
       /// </summary>
       /// <returns></returns>
        public IList<Director> GetAllDirectorInfoListDAL()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT distinct [DirectorName],[DirectorId]");
            strSql.Append(" FROM [VTPhoneDB].[dbo].[VPMS.VitualPhone] where DirectorId!=''");
            List<Director> list = new List<Director>();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }

        private Director ReaderBind(IDataReader dataReader)
        {
            Director director = new Director {
                Id = dataReader["DirectorId"].ToString(),
                Name = dataReader["DirectorName"].ToString()
            };
            return director;
        }
        #endregion

        /// <summary>
        /// 根据虚拟号码找到总监信息
        /// </summary>
        /// <param name="vitualPhone"></param>
        /// <returns></returns>
        public Director GetDirectorByVpDAL(string vitualPhone)
        {
            Director director = null;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT top 1 DirectorId,DirectorName,AssigneSatus");
            strSql.Append(" FROM [VPMS.VitualPhone] ");
            strSql.Append(" where VitualPhone=@VitualPhone");
            DbCommand cmd = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(cmd, "VitualPhone", DbType.String, vitualPhone);
            using (IDataReader dataReader = db.ExecuteReader(cmd))
            {
                while (dataReader.Read())
                {
                    director = ReaderBind(dataReader);
                }
            }
            return director;
        }

        #region 获取人事组织库中区董信息列表
        /// <summary>
        /// 获取人事组织库中区董信息列表
        /// </summary>
        /// <returns></returns>
        public IList<Director> GetAllDirectorListFromOASDAL()
        {
            string key = "KeyDirectorList";
            if (CacheHelper.Contains(key))
            {
                return CacheHelper.Get(key) as IList<Director>;
            }
            string filter = ConfigurationManager.AppSettings["DirectorFilter"];
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  USER_CODE,USER_NAME FROM oas.v_pub_roleUser WHERE role_Name='区域营业董事'");
            if (!string.IsNullOrEmpty(filter))
            {
                strSql.Append(" AND USER_CODE NOT IN("+filter+")");
            }
            List<Director> list = new List<Director>();
            using (IDataReader dataReader = dbOAS.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind1(dataReader));
                }
            }
            if (list.Any())
            {
                CacheHelper.AddHours(key, list, 2);
            }
            return list;
        }

        private Director ReaderBind1(IDataReader dataReader)
        {
            Director director = new Director
            {
                Id = dataReader["USER_CODE"].ToString(),
                Name = dataReader["USER_NAME"].ToString()
            };
            return director;
        }
        #endregion

    }
}
